package com.gisocn.onemap.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.gisocn.onemap.base.BaseFragment;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.base.MyApplication;
import com.gisocn.onemap.event.MessageEvent;
import com.gisocn.onemap.greendao.OfflineDownLoadFileInfoDao;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.interfaces.OnItemClickListener;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.Download;
import com.gisocn.onemap.model.LoadResponseHead;
import com.gisocn.onemap.presenter.OfflineDownloadPresenter;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.ui.activity.MainActivity;
import com.gisocn.onemap.ui.adapter.OfflineDownloadAdapter;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.view.OfflineDownloadView;
import com.google.gson.Gson;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.greenrobot.eventbus.EventBus;

/* loaded from: classes2.dex */
public class NewOfflineDownloadFragment_old extends BaseFragment<OfflineDownloadPresenter> implements OfflineDownloadView {
    public static final int EXTERNAL_STORAGE_REQ_CODE = 10;
    private static Gson gson = new Gson();
    private OfflineDownloadAdapter adapter;
    private AppConfig appConfig;
    OfflineDownloadPresenter presenter;
    @BindView(R.id.recycle_offline_download)
    RecyclerView recycleOfflineDownload;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    Unbinder unbinder;
    private ArrayList<Download.NewItem> xmlList = new ArrayList<>();
    ArrayList<Download.NewItem> downloadInfoList = new ArrayList<>();
    private Map<String, Download.NewItem> dataMaps = new LinkedHashMap();
    private OfflineDownLoadFileInfoDao fileInfoDao = MyApplication.getInstances().getDaoSession().getOfflineDownLoadFileInfoDao();
    boolean isShowProgress = false;
    Handler handler = new Handler() { // from class: com.gisocn.onemap.ui.fragment.NewOfflineDownloadFragment_old.3
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i == 1) {
                NewOfflineDownloadFragment_old.this.dismissProgressDialog();
                NewOfflineDownloadFragment_old.this.initView();
                Log.e("xmlList.size()", NewOfflineDownloadFragment_old.this.xmlList.size() + "");
            } else if (i == 2) {
                NewOfflineDownloadFragment_old.this.adapter.notifyItemChanged(message.arg2, 0);
                if (message.arg1 == 100) {
                    NewOfflineDownloadFragment_old.this.adapter.notifyDataSetChanged();
                    EventBus.getDefault().post(new MessageEvent(Constant.MESSAGE_EVENT_UPDATA_LAYER_DATA));
                }
            } else if (i == 3) {
                ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(message.arg1)).setVersionDesc("已是最新版本");
            } else if (i != 4) {
            } else {
                NewOfflineDownloadFragment_old.this.dismissProgressDialog();
                NewOfflineDownloadFragment_old.this.showToast("下载失败");
            }
        }
    };

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDownLoadSuccess(String str, int i) {
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void onInvisible() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_offline_download_old, viewGroup, false);
        this.unbinder = ButterKnife.bind(this, inflate);
        this.tvTitle.setText(R.string.offline_download);
        this.tvRight.setVisibility(8);
        initData();
        this.isShowProgress = true;
        return inflate;
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void initData() {
        L.e("------------------------aaaaaa");
        this.appConfig = ((MainActivity) getActivity()).getAppConfig();
        this.presenter = createPresenter();
        if (this.appConfig.getDownload().getItems() == null || this.appConfig.getDownload().getItems().size() <= 0) {
            return;
        }
        this.xmlList.clear();
        this.dataMaps.clear();
        Iterator<Download.NewItem> it = this.appConfig.getDownload().getItems().iterator();
        while (it.hasNext()) {
            Download.NewItem next = it.next();
            next.setVersionDesc("未下载");
            String str = next.getcData().trim().split("/")[next.getcData().trim().split("/").length - 1];
            if (!this.dataMaps.containsKey(str)) {
                if (next.getType().equals("shapefile")) {
                    File file = new File(Constant.SAVE_PATH_SHP);
                    if (file.exists()) {
                        File[] listFiles = file.listFiles();
                        if (listFiles == null) {
                            next.setVersionDesc("未下载");
                        } else if (listFiles.length > 0) {
                            if (listFiles.length == 1 && listFiles[0].getName().contains(".temp")) {
                                next.setVersionDesc("未下载");
                            } else {
                                next.setLastModifiedTime(Long.valueOf(file.lastModified()));
                            }
                        } else {
                            next.setVersionDesc("未下载");
                        }
                    } else {
                        next.setVersionDesc("未下载");
                    }
                }
                this.dataMaps.put(str, next);
            }
        }
        L.e("本地文件长度:" + this.presenter.getLocalPathFile(Constant.SAVE_PATH_TPK, null).size());
        ArrayList arrayList = new ArrayList();
        arrayList.addAll(this.presenter.getLocalPathFile(Constant.SAVE_PATH_TPK, null));
        Iterator it2 = arrayList.iterator();
        while (it2.hasNext()) {
            File file2 = (File) it2.next();
            if (this.dataMaps.containsKey(file2.getName())) {
                this.dataMaps.get(file2.getName()).setLastModifiedTime(Long.valueOf(file2.lastModified()));
            }
        }
        this.xmlList.addAll(this.dataMaps.values());
        if (this.isShowProgress) {
            showProgressDialog();
        }
        if (ActivityCompat.checkSelfPermission(getActivity(), "android.permission.WRITE_EXTERNAL_STORAGE") == 0) {
            new Thread(new Runnable() { // from class: com.gisocn.onemap.ui.fragment.NewOfflineDownloadFragment_old.1
                @Override // java.lang.Runnable
                public void run() {
                    NewOfflineDownloadFragment_old.this.presenter.getDownLoadInfo(((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(0)).getcData().trim(), 0);
                }
            }).start();
        } else {
            showToast("请设置文件读写权限");
        }
    }

    void initView() {
        dismissProgressDialog();
        Log.e("init", "init----------下载测试----------");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(1);
        this.recycleOfflineDownload.setLayoutManager(linearLayoutManager);
        OfflineDownloadAdapter offlineDownloadAdapter = new OfflineDownloadAdapter(this.xmlList, getActivity());
        this.adapter = offlineDownloadAdapter;
        this.recycleOfflineDownload.setAdapter(offlineDownloadAdapter);
        final ArrayList arrayList = new ArrayList();
        this.adapter.setOnItemClickListener(new OnItemClickListener() { // from class: com.gisocn.onemap.ui.fragment.NewOfflineDownloadFragment_old.2
            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, final int i) {
                String str;
                Log.e("onItemClick", "onItemClick" + ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getcData());
                if (arrayList.contains(Integer.valueOf(i))) {
                    Message message = new Message();
                    message.what = 2;
                    NewOfflineDownloadFragment_old newOfflineDownloadFragment_old = NewOfflineDownloadFragment_old.this;
                    message.arg1 = newOfflineDownloadFragment_old.isNull(((Download.NewItem) newOfflineDownloadFragment_old.xmlList.get(i)).getCurrentLength()) ? 0 : Integer.parseInt(((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getCurrentLength());
                    message.arg2 = i;
                    NewOfflineDownloadFragment_old.this.handler.sendMessage(message);
                    return;
                }
                arrayList.add(Integer.valueOf(i));
                Message message2 = new Message();
                String trim = ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getcData().trim();
                L.e("xmlList.get(position).getCurrentLength" + ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getCurrentLength());
                L.e("xmlList.get(position).getCurrentLength" + ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getSize());
                String str2 = trim.split("/")[trim.split("/").length + (-1)] + ".temp";
                L.e("xmlList.get(position).getCurrentLength" + str2);
                if (str2.contains(".tpk")) {
                    str = Constant.SAVE_PATH_TPK + str2;
                } else {
                    str = Constant.SAVE_PATH_SHP + str2;
                }
                File file = new File(str);
                if (file.exists()) {
                    L.e("存在临时文件大小" + file.length() + "---" + Float.parseFloat(((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getSize()));
                    r0 = (int) ((((float) file.length()) / Float.parseFloat(((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getSize())) * 100.0f);
                    StringBuilder sb = new StringBuilder();
                    sb.append("currentLength===========");
                    sb.append(((float) file.length()) / Float.parseFloat(((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getSize()));
                    L.e(sb.toString());
                }
                ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).setCurrentLength("" + r0);
                L.e("开始进度currentLength" + r0);
                message2.what = 2;
                message2.arg2 = i;
                message2.arg1 = r0;
                NewOfflineDownloadFragment_old.this.handler.sendMessage(message2);
                NewOfflineDownloadFragment_old.this.presenter.getDownLoading(((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).getcData().trim(), new DownLoadListener() { // from class: com.gisocn.onemap.ui.fragment.NewOfflineDownloadFragment_old.2.1
                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onStart() {
                        L.e("onStart____________________");
                    }

                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onProgress(int i2) {
                        NewOfflineDownloadFragment_old.this.dismissProgressDialog();
                        ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).setCurrentLength("" + i2);
                        if (i2 == 100) {
                            ((Download.NewItem) NewOfflineDownloadFragment_old.this.xmlList.get(i)).setVersionDesc("已是最新版本");
                        }
                        Message message3 = new Message();
                        message3.what = 2;
                        message3.arg1 = i2;
                        message3.arg2 = i;
                        NewOfflineDownloadFragment_old.this.handler.sendMessage(message3);
                    }

                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onFinish(String str3) {
                        Message message3 = new Message();
                        message3.what = 3;
                        message3.arg1 = i;
                        NewOfflineDownloadFragment_old.this.handler.sendMessage(message3);
                    }

                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onFailure() {
                        NewOfflineDownloadFragment_old.this.handler.sendEmptyMessage(4);
                    }
                });
            }

            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, int i, int i2) {
                System.out.println("asdfasdf");
            }
        });
    }

    @Override // androidx.fragment.app.Fragment
    public void onStop() {
        super.onStop();
        dismissProgressDialog();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseFragment
    public OfflineDownloadPresenter createPresenter() {
        return new OfflineDownloadPresenter(this, getActivity());
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public void onDestroyView() {
        super.onDestroyView();
        this.unbinder.unbind();
        OfflineDownloadPresenter offlineDownloadPresenter = this.presenter;
        if (offlineDownloadPresenter != null) {
            offlineDownloadPresenter.closeDownloading();
        }
        this.handler.removeCallbacksAndMessages(null);
    }

    @OnClick({R.id.tv_close, R.id.tv_right})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.tv_close) {
            return;
        }
        getActivity().findViewById(R.id.viewPager).setVisibility(8);
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDownLoadInfoSuccess(LoadResponseHead loadResponseHead, int i) {
        String str = this.xmlList.get(i).getcData().trim().split("/")[this.xmlList.get(i).getcData().trim().split("/").length - 1];
        if (!isNull(loadResponseHead.getLast_modified())) {
            this.dataMaps.get(str).setVersion(String.valueOf(new Date(loadResponseHead.getLast_modified()).getTime()));
        }
        if (!isNull(loadResponseHead.getContent_length())) {
            this.dataMaps.get(str).setSize(loadResponseHead.getContent_length());
        }
        int i2 = i + 1;
        if (i2 <= this.xmlList.size() - 1) {
            if (this.xmlList.get(i2).getcData() != null) {
                this.presenter.getDownLoadInfo(this.xmlList.get(i2).getcData().trim(), i2);
                return;
            }
            int i3 = i2 + 1;
            this.presenter.getDownLoadInfo(this.xmlList.get(i3).getcData().trim(), i3);
            return;
        }
        Iterator<Download.NewItem> it = this.xmlList.iterator();
        while (it.hasNext()) {
            Download.NewItem next = it.next();
            if (next.getLastModifiedTime() == null) {
                next.setVersionDesc("未下载");
            } else if (next.getVersion() == null) {
                next.setVersionDesc("已是最新版本");
            } else if (next.getLastModifiedTime().longValue() < Long.valueOf(Long.parseLong(next.getVersion())).longValue()) {
                next.setVersionDesc("有更新版本");
            } else {
                next.setVersionDesc("已是最新版本");
            }
        }
        this.handler.sendEmptyMessage(1);
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDataFail(String str) {
        hideLoading();
        showToast(str);
    }

    @Override // com.gisocn.onemap.base.BaseFragment, com.gisocn.onemap.base.BaseView
    public void showLoading() {
        showProgressDialog();
        Log.e("showLoading", "showLoading");
    }

    @Override // com.gisocn.onemap.base.BaseFragment, com.gisocn.onemap.base.BaseView
    public void hideLoading() {
        dismissProgressDialog();
        Log.e("hideLoading", "hideLoading");
    }
}
